Attribute VB_Name = "Module1"
Option Explicit

Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long)

Public Type typeFakeLaserLevel
 nWidth As Long '2-16
 nHeight As Long '2-16
 nObjectCount As Long '0-63
 nHintCount As Long '(0)1-4
 bData(255) As Byte
 bObjectPos(63) As Byte
 bObjectType(63) As Byte
End Type

Public Function LoadLevel(ByVal fnum As Long, ByRef ret As typeFakeLaserLevel) As Boolean
Dim t As typeFakeLaserLevel
Dim i As Long, j As Long
Dim nMask As Long
Dim b As Byte
'///get map size
Get fnum, , b
t.nWidth = (b And &HF&) + 1&
t.nHeight = (b \ &H10&) + 1&
If t.nWidth <= 1 Or t.nHeight <= 1 Then Exit Function
'///get map data
nMask = &H100&
For j = 0 To t.nHeight - 1
 For i = 0 To t.nWidth - 1
  If nMask >= &H100& Then
   nMask = 1
   Get fnum, , b
  End If
  t.bData(j * 16& + i) = (b And nMask) <> 0 And 1&
  nMask = nMask + nMask
 Next i
Next j
'///get object count
Get fnum, , b
t.nObjectCount = b And &H3F&
t.nHintCount = (b \ &H40&) + 1&
For i = 0 To t.nObjectCount - 1
 Get fnum, , t.bObjectPos(i)
 Get fnum, , t.bObjectType(i)
Next i
'///
ret = t
LoadLevel = True
End Function

Public Function SaveLevel(ByVal fnum As Long, ByRef t As typeFakeLaserLevel) As Boolean
Dim i As Long, j As Long
Dim b As Byte
Dim nMask As Long
'///save map size
If t.nWidth >= 2 And t.nWidth <= 16 And t.nHeight >= 2 And t.nHeight <= 16 And t.nObjectCount <= 63 Then
 b = (t.nWidth - 1&) Or ((t.nHeight - 1&) * &H10&)
 Put fnum, , b
Else
 Debug.Assert False
 Exit Function
End If
'///save map data
b = 0
nMask = 1
For j = 0 To t.nHeight - 1
 For i = 0 To t.nWidth - 1
  Debug.Assert t.bData(j * 16& + i) <= 1
  b = b Or (t.bData(j * 16& + i) = 1 And nMask)
  nMask = nMask + nMask
  If nMask >= &H100& Then
   nMask = 1
   Put fnum, , b
   b = 0
  End If
 Next i
Next j
If nMask > 1 Then
 Put fnum, , b
End If
'///save object data
b = t.nObjectCount Or (((t.nHintCount - 1) And 3&) * &H40&)
Put fnum, , b
For i = 0 To t.nObjectCount - 1
 Put fnum, , t.bObjectPos(i)
 Put fnum, , t.bObjectType(i)
Next i
'///
SaveLevel = True
End Function
